<?php

namespace Tests\Browser\Pages\H5\Contracts;

use App\Models\Customer\Customer;
use App\Models\OpenWechat\ScanEvent;
use App\Models\OpenWechat\WechatUser;
use Tests\Browser\Pages\Page;

abstract class H5AbstractPage extends Page
{
    protected $wechatUser;
    /**
     * @var Customer
     */
    private $customer;
    /**
     * @var ScanEvent
     */
    private $scanEvent;

    public function __construct(WechatUser $wechatUser,
                                Customer $customer,
                                ScanEvent $scanEvent)
    {
        $this->wechatUser = $wechatUser;
        $this->customer = $customer;
        $this->scanEvent = $scanEvent;
    }

    /**
     * Get the URL for the page.
     *
     * @return string
     */
    public function url()
    {
        return $this->spaRouteUrl();
    }

    protected function spaRouteUrl(string $hashPath = "/"): string
    {
        $qs = $this->buildQueryString();

        return "/wx-system/?$qs#{$hashPath}";
    }

    /**
     * Get the element shortcuts for the page.
     *
     * @return array
     */
    public function elements()
    {
        return [
            '@user-info-btn'   => '.info',
            '@user-center-vue' => '.user-info',
        ];
    }

    /**
     * @return string
     */
    protected function buildQueryString(): string
    {
        $qs = http_build_query([
            'openid'        => $this->wechatUser->openid,
            'customer_id'   => $this->customer->id,
            'scan_event_id' => $this->scanEvent->id,
        ]);

        return $qs;
    }
}
